import { CustomPage } from '/engine/index';
import { login, getUserInfo, getSmsCode } from '../../services/user';
import { CLIENT_ID } from '../../services/request';

CustomPage({
  data: {
    isQuickLogin: true,
    isCheckPrivacy: false,
    disableCode: false,
    countdown: 60,
    showPrivacyModal: false,
    phoneNumber: '',
    validityCode: '',
    isValidityCode: false,
  },
  async onload() {
    const userInfo = await getUserInfo();
    if (userInfo.data) {
      wx.redirectTo({
        url: '/pages/chat/index',
      });
    }
  },
  onClickPrivacy() {
    this.setData({ isCheckPrivacy: !this.data.isCheckPrivacy });
  },
  async onGetPhoneNumber(e) {
    if (!this.data.isCheckPrivacy) {
      this.setData({ showPrivacyModal: true });
      return;
    }
    if (e.detail.errMsg !== 'getPhoneNumber:ok') {
      return;
    }
    wx.login({
      success: async res => {
        if (res.code) {
          const resp = await login({
            xcxCode: res.code,
            clientId: CLIENT_ID,
            grantType: 'xcx',
            phoneCode: e.detail.code,
          });
          wx.setStorageSync('access_token', resp?.data?.access_token);
          wx.redirectTo({ url: '/pages/create-profile/index' });
        }
      },
    });
  },
  onChangeLoginType() {
    this.setData({ isQuickLogin: !this.data.isQuickLogin });
  },
  async onSendCode() {
    const { phoneNumber } = this.data;
    if (this.data.disableCode) {
      return;
    }
    if (phoneNumber.length < 11) {
      wx.showToast({ title: '请输入正确的手机号', icon: 'none' });
      return;
    }
    this.setData({ disableCode: true, isValidityCode: true });

    await getSmsCode(phoneNumber);
    const timer = setInterval(() => {
      let { countdown } = this.data;
      countdown--;

      if (countdown <= 0) {
        clearInterval(timer);
        this.setData({
          disableCode: false,
          countdown: 60,
        });
      } else {
        this.setData({ countdown });
      }
    }, 1000);
  },
  async onClickLogin() {
    if (!this.data.isCheckPrivacy) {
      this.setData({ showPrivacyModal: true });
      return;
    }
    if (!this.data.phoneNumber || !this.data.validityCode) {
      wx.showToast({
        title: '请检查输入的手机号和验证码不能为空',
        icon: 'none',
      });
      return;
    }
    if (!this.data.isValidityCode) {
      wx.showToast({ title: '请验证手机成功后再试', icon: 'none' });
      return;
    }

    wx.login({
      success: async res => {
        if (res.code) {
          const resp = await login({
            xcxCode: res.code,
            clientId: '5d379b820a5562b36ec5d48caa741aaa',
            grantType: 'xcx',
            phone: this.data.phoneNumber,
            phoneAuthCode: this.data.validityCode,
          });
          wx.setStorageSync('access_token', resp?.data?.access_token);
          wx.redirectTo({ url: '/pages/create-profile/index' });
        }
      },
    });
  },
  onCancel() {
    this.setData({ showPrivacyModal: false });
  },
  onConfirm() {
    this.setData({ showPrivacyModal: false });
  },
  onNavigateToUser() {
    wx.navigateTo({ url: '/pages/user-agreement/index' });
  },
  onNavigateToPrivacy() {
    wx.navigateTo({ url: '/pages/privacy-policy/index' });
  },
  onChangePhone(e) {
    this.setData({ phoneNumber: e.detail.value });
  },
  onChangeCode(e) {
    this.setData({ validityCode: e.detail.value });
  },
});
